package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.entity.FolderPO;
import com.ajmd.onlineexam.entity.StudentPO;
import com.ajmd.onlineexam.entity.TeacherPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.FolderService;
import com.ajmd.onlineexam.service.QuestionBankService;
import com.ajmd.onlineexam.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author ajmd
 * @since 2023/3/25 20:56
 */
@Api("文件夹分类")
@RestController
@RequestMapping("/folder")
public class FolderController {
    @Resource
    private FolderService folderService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private QuestionBankService questionBankService;

    @PostMapping("/add")
    public CommonResult addFolder(@RequestBody FolderPO folderPO){
        if(ObjectUtils.isEmpty(folderPO.getFolderType()) || ObjectUtils.isEmpty(folderPO.getTypeName())){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = folderService.addFolder(folderPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_ADD_ERR).appendMessage("无新增目录");
        }
        return CommonResult.ok();
    }

    @GetMapping("/select")
    public CommonResult findFolderById(@RequestParam String id){
//        if(ObjectUtils.isEmpty(id)){
//            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR).appendMessage("id不能为空");
//        }
        FolderPO folder = folderService.findFolderById(id);
        if(ObjectUtils.isEmpty(folder)){
            return CommonResult.error().message("没有该分类");
        }
        return CommonResult.ok().data("folder",folder);
    }

    @GetMapping("/findFolders")
    public CommonResult findFolders(@RequestParam String account,
                                    @RequestParam String folderId,
                                    @RequestParam Integer folderType,
                                    @RequestParam Integer userType,
                                    @RequestParam Integer folderRange){
        if(ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(folderType) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        //权限校验
//        if(userType.equals(StaticFinalValue.STUDENT)){
//            return CommonResult.error(AppExceptionCodeEnum.STUDENT_NOT_AUTHORITY);
//        }else if(userType.equals(StaticFinalValue.TEACHER)){
//            TeacherPO teacher = teacherService.findTeacherById(createPerson);
//            if(ObjectUtils.isEmpty(teacher)){
//                return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
//            }
//        }else{
//            System.out.println();
//        }

        List<FolderPO> folders = folderService.findFolders(account, folderId, folderType, userType, folderRange);
        return CommonResult.ok().data("pageData",folders);
    }

    @DeleteMapping("/delete")
    public CommonResult deleteFolder(@RequestParam String id){
        if(ObjectUtils.isEmpty(id)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR).appendMessage("id不能为空");
        }
        int num = folderService.deleteFolder(id);
        if(num==-1){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_DELETE_ERR);
        }
        return CommonResult.ok();
    }


//    @ApiOperation("修改分类信息")
    @PutMapping("/update")
    public CommonResult updateFolderByPO(@RequestBody FolderPO folderPO){
        //私有分类改公开无限制，但公开分类改私有需要判断相关题库/试卷和子分类是否存在公开，存在则不允许改私有
        FolderPO oldFolder = folderService.findFolderById(folderPO.getId());
        if(oldFolder.getPublicRange().equals(StaticFinalValue.PUBLIC) && folderPO.getPublicRange().equals(StaticFinalValue.NOT_PUBLIC)){
            Boolean folderPublic = folderService.hasPublicChildrenFolder(folderPO.getId());
            if(folderPublic){
                return CommonResult.error(AppExceptionCodeEnum.EXIST_CHILDREN_FOLDER_PUBLIC);
            }
            Boolean associationPublic = folderService.hasAssociationPublic(folderPO.getId(),folderPO.getFolderType());
            if(associationPublic){
                return CommonResult.error(AppExceptionCodeEnum.EXIST_QUESTION_BANK_PUBLIC);
            }
        }
        int num = folderService.folderUpdate(folderPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("未更新任何分类信息");
        }
        return CommonResult.ok("分类信息更新成功");
    }
}
